﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

$.tree.plugins.checkbox.check = function (n) {
    if (!n) return false;
    var t = $.tree.reference(n);
    n = t.get_node(n);
    if (n.children("a").hasClass("checked")) return true;
    n.children("a").removeClass("unchecked undetermined").addClass("checked");
    return true;
}




$(function () {
    var _CategoryTree = {
        OpenPCategoryTree: function () {
            $.tree.reference("#PCategoryTree").open_all();
        },
        ClosePCategoryTree: function () {
            $.tree.reference("#PCategoryTree").close_all();
        },
        CreatePCategoryTree: function () {
            var values = {};
            $("#PCategoryTree").tree({
                types: {
                    "default": {
                        draggable: true
                    }
                },
                ui: {
                    animation: 150,
                    theme_name: "checkbox"
                },
                plugins: {
                    checkbox: {}
                },
                callback: {
                    onsearch: function (n, t) {
                        $.each(n, function (i, o) {
                            $(o).addClass('search');
                        })
                    }
                }

            });
        }
    }
    _CategoryTree.CreatePCategoryTree();
    _CategoryTree.OpenPCategoryTree();


    $("#Insert_Btn").click(function () {
        var select = $.tree.plugins.checkbox.get_checked($.tree.reference("#PCategoryTree"));
        var flag;
        var _ucategory = '';
        $.each(select, function (i, n) {
            _ucategory += n.id + '|';
        })
        _ucategory = _ucategory.substring(0, _ucategory.length - 1);
        if (select.length > 0) {
            if (select.length > 1) {
                alert("只能选择一项"); return false;
            }
            else {
                alert(_ucategory);
            }
        }
    });

    //删除
    $("#Delete_Btn").click(function () {
        var _ucategory = '';
        var flag = true;
        $.each($.tree.plugins.checkbox.get_checked($.tree.reference("#PCategoryTree")), function (i, n) {
            if (n.id == "1") {
                flag = false;
            }
            _ucategory += n.id + '|';
        })
        if (_ucategory.length > 0) {
            if (flag) {
                if (confirm('删除父分类将同时删除其所有子分类，确认删除？')) {
                    _ucategory = _ucategory.substring(0, _ucategory.length - 1);
                    alert(_ucategory);
                }
            }
            else {
                alert("删除节点中包含根节点，此节点不能删除！");
            }
        }
        else {
            alert("请选择要删除的项目分类");
        }
    })
});